﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Abs.Core.DAL.AdoNet;
using Abs.Core.Models.Room;

namespace Abs.Core.Management
{
    public class RoomManagement
    {
        internal RoomManagement()
        {
        }

        [DataObjectMethod(DataObjectMethodType.Insert)]
        public bool CreateRoom(string roomName)
        {
            using(var roomRepository = new RoomRepository())
            {
                return roomRepository.Create(new Room {Name = roomName});
            }//end of using(var roomRepository = new RoomRepository())
        }//end of public bool CreateRoom(string roomName)

        /// <summary>
        /// Get the latest RoomId. Usually call this property after
        /// inserting a new Room.
        /// </summary>
        public int RoomIdentity
        {
            get
            {
                using(var roomRepository = new RoomRepository())
                {
                    return roomRepository.Identity;
                }//end of using(var roomRepository = new RoomRepository())
            }//end of get RoomIdentity
        }//end of public int RoomIdentity

        [DataObjectMethod(DataObjectMethodType.Update)]
        public bool UpdateRoom(int roomId, string roomName)
        {
            using(var roomRepository = new RoomRepository())
            {
                return roomRepository.Update(new Room
                                      {
                                          Name = roomName,
                                          RoomId = roomId
                                      });
            }//end of using(var roomRepository = new RoomRepository())
        }//end of public bool UpdateRoom(int roomId, string roomName)

        [DataObjectMethod(DataObjectMethodType.Select)]
        public Room SelectRoom(int roomId)
        {
            using(var roomRepository = new RoomRepository())
            {
                return roomRepository.Select(new object[] {roomId});
            }//end of using(var roomRepository = new RoomRepository())
        }//end of public Room SelectRoom(int roomId)

        [DataObjectMethod(DataObjectMethodType.Fill)]
        public IList<Room> SelectAllRoom()
        {
            using (var roomRepository = new RoomRepository())
            {
                return roomRepository.SelectAll();
            }//end of using (var roomRepository = new RoomRepository())
        }//end of public IList<Room> SelectAllRoom()

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public bool DeleteRoom(int roomId)
        {
            using(var roomRepository = new RoomRepository())
            {
                return roomRepository.Delete(new object[] {roomId});
            }//end of using(var roomRepository = new RoomRepository())
        }//end of public bool DeleteRoom(int roomId)

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public bool DeleteRoomScheduleByRoomId(int roomId)
        {
            using(var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.DeleteRoomScheduleByRoomId(roomId);
            }//end of using(var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public bool DeleteRoomScheduleByRoomId()

        [DataObjectMethod(DataObjectMethodType.Insert)]
        public bool CreateRoomSchedule(int roomId, int dayId, 
                                        string startTime, 
                                        string endTime)
        {
            var beginTime = DateTime.Parse(startTime);
            var finishTime = DateTime.Parse(endTime);
            var roomSchedule = new RoomSchedule
                               {
                                   DayId = dayId,
                                   RoomId = roomId,
                                   StartTime = beginTime,
                                   EndTime = finishTime
                               };
            using(var roomRepository = new RoomScheduleRepository())
            {
                return roomRepository.Create(roomSchedule);
            }//end of using(var roomRepository = new RoomRepository())
        }//end of public bool CreateRoomSchedule

        [DataObjectMethod(DataObjectMethodType.Update)]
        public bool EditRoomSchedule(int roomId, int dayId, 
                                     string startTime, string endTime)
        {
            var beginTime = DateTime.Parse(startTime);
            var finishTime = DateTime.Parse(endTime);
            var roomSchedule = new RoomSchedule
            {
                DayId = dayId,
                RoomId = roomId,
                StartTime = beginTime,
                EndTime = finishTime
            };

            using(var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.Update(roomSchedule);
            }//end of using(var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public bool EditRoomSchedule

        [DataObjectMethod(DataObjectMethodType.Select)]
        public RoomSchedule SelectRoomSchedule(int roomId, int dayId)
        {
            using(var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.Select(new object[] {roomId, dayId});
            }//end of using(var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public void SelectRoomSchedule(int roomId, int dayId)

        [DataObjectMethod(DataObjectMethodType.Fill)]
        public IList<RoomSchedule> SelectAllRoomSchedules()
        {
            using (var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.SelectAll();
            }//end of using (var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public IList<RoomSchedule> SelectAllRoomSchedules()

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public bool DeleteRoomSchedule(int roomId, int dayId)
        {
            using(var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.Delete(new object[] {roomId, dayId});
            }//end of using(var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public bool DeleteRoomSchedule(int roomId, int dayId)

        public IList<RoomSchedule> GetRoomsOnDay(int id)
        {
            using(var roomScheduleRepository = new RoomScheduleRepository())
            {
                return roomScheduleRepository.GetRoomsOnDay(id);
            }//end of using(var roomScheduleRepository = new RoomScheduleRepository())
        }//end of public void GetRoomsOnDay(int id)
    }//end of public class RoomManagement
}//end of namespace ABS.Management